package com.dys.testjdk15;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @className: com.dys.testjdk15.HiddenClass
 * @description: 隐藏类
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/09 下午9:37
 */
public class HiddenClass {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) {
        Animal animal = new Dog();
        // Animal animal = new Animal();
        // Animal an = (Animal) Proxy.newProxyInstance(null,null,null);
        // Animal an = (Animal) Proxy.newProxyInstance(animal.getClass().getClassLoader(), animal.getClass().getInterfaces(), new InvocationHandler() {
        //     @Override
        //     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //         System.out.println("方法执行前......");
        //         return method.invoke(animal, args);
        //     }
        // });
        Animal an = (Animal) Proxy.newProxyInstance(animal.getClass().getClassLoader(), new Class[]{Animal.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("方法执行前......");
                return method.invoke(animal, args);
            }
        });
        an.eat();
        System.out.println(an.getClass().getName());
    }

}

// class Animal {
//     public void eat() {
//         System.out.println("Animal eat");
//     }
// }
interface Animal {
    public void eat();
}

class Dog implements Animal {
    @Override
    public void eat() {
        System.out.println("Dog eat ......");
    }
}